package cn.kgc.newmanager.controller;

import cn.kgc.newmanager.entity.ResultEntity;
import cn.kgc.newmanager.pojo.Detail;
import cn.kgc.newmanager.service.DetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 郭祥伟
 */
@RestController
public class DetailController {
    @Resource
    DetailService detailService;
    @RequestMapping("/selectDetail")
    public ResultEntity<PageInfo<Detail>> selectDetail(
            @RequestParam(value = "title",required = false,defaultValue = "") String title,
            @RequestParam(value = "categoryId",required = false,defaultValue = "0") Integer categoryId,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "4") Integer pageSize
    ){
        PageInfo<Detail> pageInfo=detailService.select(pageNum,pageSize,title,categoryId);
        return ResultEntity.successWithoutData(pageInfo);
    }
    @RequestMapping("/saveDetail")
    public ResultEntity<String> saveDetail(@RequestBody Detail detail){
        try {
            Integer integer=0;
            if (detail.getId() == null) {
                //添加
                integer = detailService.add(detail);
            } else {
                //删除
                integer = detailService.update(detail);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex) {
            return ResultEntity.failed(ex.getMessage());
        }
    }

    //根据id查询图书
    @RequestMapping("/getDetailById")
    public ResultEntity<Detail> getDetailById(Integer id){
        Detail detail=detailService.selectById(id);
        return ResultEntity.successWithoutData(detail);
    }

    //根据id删除图书
    @RequestMapping("/delDetailById")
    public ResultEntity<String> delDetailById(Integer id){
        try{
            Integer integer=detailService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }
}
